package com.example.kotlin_retrofit.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlin_retrofit.R
import com.example.kotlin_retrofit.entity.ArticleData
import kotlinx.android.synthetic.main.item_article.view.*

class ArticleAdapter(var mContext: Context, var mList: List<ArticleData>) :
    RecyclerView.Adapter<ArticleAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var mTvArticle: TextView = view.tv_article
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.item_article,
                parent,
                false
            )
        )
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mTvArticle.text = mList[position].title
        //设置点击事件，Lambda 表达式用着真的很舒服
        holder.mTvArticle.setOnClickListener {
            Toast.makeText(
                mContext,
                mList[position].title,
                Toast.LENGTH_SHORT
            ).show()
        }
    }
}